#include "movingunit.h"

MovingUnit::MovingUnit() : Unit()
{
    init();
}

MovingUnit::MovingUnit(QRect &geometry) : Unit(geometry)
{
    init();
}

int MovingUnit::getBaseSpeed() {return this->m_baseSpeed;}
int MovingUnit::getRealSpeed() {return this->m_realSpeed;}
int MovingUnit::getDirection() {return this->m_direction;}

void MovingUnit::setBaseSpeed(int speed) {this->m_baseSpeed = speed;}
void MovingUnit::setRealSpeed(int speed) {this->m_realSpeed = speed;}
void MovingUnit::setDirection(int direction) {this->m_direction = direction;}

void MovingUnit::init()
{
    setBaseSpeed(7);
    setRealSpeed(7);
    setLives(1);

    m_direction = Down;
}


void MovingUnit::loadPixmaps()
{
    if(m_pixmaps.count() < 0) return;

    this->m_pixmaps.resize(4);

    for(int direction = 0; direction < 4; direction++)
    {
        this->m_pixmaps[direction].resize(4);
        for(int step = 0; step < 4; step++)
        {
            switch(direction)
            {
                case Left :
                    m_pixmaps[direction][step].load("./sprites/" + m_leftSprite + QString::number(step+1) + ".png");
                    break;
                case Up :
                    m_pixmaps[direction][step].load("./sprites/" + m_upSprite + QString::number(step+1) + ".png");
                    break;
                case Right :
                    m_pixmaps[direction][step].load("./sprites/" + m_rightSprite + QString::number(step+1) + ".png");
                    break;
                case Down :
                    m_pixmaps[direction][step].load("./sprites/" + m_downSprite + QString::number(step+1) + ".png");
                    break;
            }
        }
    }
}


int MovingUnit::getReverseDirection(int d)
{
    switch (d)
    {
        case  Left: return MovingUnit::Right;
        case  Up: return MovingUnit::Down;
        case  Right: return MovingUnit::Left;
        case  Down: return MovingUnit::Up;
        default: ;
    }
    return -1;
}

void MovingUnit::move(int left, int top)
{
    m_lastLeft = m_geometry.left();
    m_lastTop = m_geometry.top();
    m_geometry.moveTo(left, top);

    if(m_direction < m_pixmaps.count()) //A CHANGER
        m_step = (m_step + 1) % m_pixmaps[m_direction].count();
}

void MovingUnit::moveDown()
{
    setDirection(Down);
    move(m_geometry.left(), m_geometry.top() + m_realSpeed);
}

void MovingUnit::moveUp()
{
    setDirection(Up);
    move(m_geometry.left(), m_geometry.top() - m_realSpeed);
}

void MovingUnit::moveLeft()
{
    setDirection(Left);
    move(m_geometry.left() - m_realSpeed, m_geometry.top());
}

void MovingUnit::moveRight()
{
    setDirection(Right);
    move(m_geometry.left() + m_realSpeed, m_geometry.top());
}

void MovingUnit::moveTo(int d)
{
    switch (d)
    {
        case  Left: moveLeft(); break;
        case  Up: moveUp(); break;
        case  Right: moveRight(); break;
        case  Down: moveDown(); break;
        default: ;
    }
}

void MovingUnit::truncMove(int left, int top, int right, int bottom)
{
    switch(m_direction)
    {
        case Left :
            m_geometry.moveLeft(right + 1);
            break;
        case Up :
            m_geometry.moveTop(bottom + 1);
            break;
        case Right :
            m_geometry.moveRight(left - 1);
            break;
        case Down :
            m_geometry.moveBottom(top - 1);
            break;
    }
}

void MovingUnit::cancelMove()
{
    move(m_lastLeft, m_lastTop);
}

void MovingUnit::truncCancelMove(int left, int top, int right, int bottom)
{
    int d = getDirection();
    setDirection(getReverseDirection(d));
    cancelMove();
    truncMove(left, top, right, bottom);
    setDirection(d);
}


void MovingUnit::serializeDebug(QDebug &dbg)
{
    Unit::serializeDebug(dbg);
    dbg << "MovingUnit ( ";
    dbg << "BaseSpeed:" << m_baseSpeed
        << "RealSpeed:" << m_realSpeed
        << "Direction:" << m_direction;
    dbg << ")";
}

void MovingUnit::serializeIn(QDataStream &in)
{    
    Unit::serializeIn(in);
    in >> m_baseSpeed;
    in >> m_realSpeed;
    in >> m_direction;
}

void MovingUnit::serializeOut(QDataStream &out)
{
    Unit::serializeOut(out);
    out << (qint32)m_baseSpeed;
    out << (qint32)m_realSpeed;
    out << (qint32)m_direction;
}
